package redis

import "context"

// 向集合添加一个或多个成员
func (h *Handler) SAdd(key string, members ...interface{}) (count int64, err error) {
	count, err = h.Client.SAdd(context.Background(), key, members...).Result()
	go h.Log("Set-SAdd", []interface{}{key, members}, count, err)
	return
}

// 获取集合的成员数
func (h *Handler) SCard(key string) (count int64, err error) {
	count, err = h.Client.SCard(context.Background(), key).Result()
	go h.Log("Set-SCard", key, count, err)
	return
}

// 返回第一个集合与其他集合之间的差异，切片第一个值为第一个集合键值
func (h *Handler) SDiff(keys ...string) (members []string, err error) {
	members, err = h.Client.SDiff(context.Background(), keys...).Result()
	go h.Log("Set-SDiff", keys, members, err)
	return
}

// 返回给定所有集合的差集并存储在 destination 中
func (h *Handler) SDiffStore(destination string, keys ...string) (count int64, err error) {
	count, err = h.Client.SDiffStore(context.Background(), destination, keys...).Result()
	go h.Log("Set-SDiffStore", []interface{}{destination, keys}, count, err)
	return
}

// 返回给定所有集合的交集
func (h *Handler) SInter(keys ...string) (members []string, err error) {
	members, err = h.Client.SInter(context.Background(), keys...).Result()
	go h.Log("Set-SInter", keys, members, err)
	return
}

// 返回给定所有集合的交集并存储在 destination 中
func (h *Handler) SInterStore(destination string, keys ...string) (count int64, err error) {
	count, err = h.Client.SInterStore(context.Background(), destination, keys...).Result()
	go h.Log("Set-SInterStore", []interface{}{destination, keys}, count, err)
	return
}

// 判断 member 元素是否是集合 key 的成员
func (h *Handler) SIsMember(key string, member string) (status bool, err error) {
	status, err = h.Client.SIsMember(context.Background(), key, member).Result()
	go h.Log("Set-SIsMember", []interface{}{key, member}, status, err)
	return
}

// 返回集合中的所有成员
func (h *Handler) SMembers(key string) (members []string, err error) {
	members, err = h.Client.SMembers(context.Background(), key).Result()
	go h.Log("Set-SMembers", key, members, err)
	return
}

// 将 member 元素从 source 集合移动到 destination 集合
func (h *Handler) SMove(source string, destination string, member string) (status bool, err error) {
	status, err = h.Client.SMove(context.Background(), source, destination, member).Result()
	go h.Log("Set-SMove", []interface{}{source, destination, member}, status, err)
	return
}

// 随机移除并返回集合中的一个元素
func (h *Handler) SPop(key string) (member string, err error) {
	member, err = h.Client.SPop(context.Background(), key).Result()
	go h.Log("Set-SPop", key, member, err)
	return
}

// 随机返回集合中一个成员
func (h *Handler) SRandMember(key string) (member string, err error) {
	member, err = h.Client.SRandMember(context.Background(), key).Result()
	go h.Log("Set-SRandMember", key, member, err)
	return
}

// 移除集合中一个或多个成员
func (h *Handler) SRem(key string, members ...string) (count int64, err error) {
	count, err = h.Client.SRem(context.Background(), key, members).Result()
	go h.Log("Set-SRem", []interface{}{key, members}, count, err)
	return
}

// 返回所有给定集合的并集
func (h *Handler) SUnion(keys ...string) (members []string, err error) {
	members, err = h.Client.SUnion(context.Background(), keys...).Result()
	go h.Log("Set-SUnion", keys, members, err)
	return
}

// 所有给定集合的并集存储在 destination 集合中
func (h *Handler) SUnionStore(destination string, keys ...string) (count int64, err error) {
	count, err = h.Client.SUnionStore(context.Background(), destination, keys...).Result()
	go h.Log("Set-SUnionStore", []interface{}{destination, keys}, count, err)
	return
}

// 迭代获取集合中的元素
func (h *Handler) SScan(key string, cursor uint64, match string, count int64) (members []string, nextCursor uint64, err error) {
	members, nextCursor, err = h.Client.SScan(context.Background(), key, cursor, match, count).Result()
	go h.Log("Set-SScan", []interface{}{key, cursor, match, count}, []interface{}{members, nextCursor}, err)
	return
}
